import Vue from 'vue'
import Vuex from 'vuex'
import VueCookie from 'vue-cookie';


Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token:'',
    userInfo:JSON.parse(VueCookie.get("userInfo"))
  },
  mutations: {
    /**
     * this.$store.commit("")来使用方法
     */

    /**
     * 设置token
     * @param state
     * @param token
     */
    set_token:(state,token) => {
      state.token=token
      //放置浏览器的localStorage中
      VueCookie.set('token', token);
    },
    /**
     * 设置user_Info
     * @param state
     * @param user
     */
    set_user:(state,user) => {
      //userInfo.identity1='if'
      state.user=user
      //sessionStorage中，会话结束即userInfo消失
       // session中不能存对象，所以要转换一下
       VueCookie.set("user",JSON.stringify(user));
    },
    /**
     * 消除信息
     * @param state
     */
    remove_info:(state)=>{
        state.token=''
        state.user={}
        VueCookie.delete('token');
        VueCookie.delete('user');
    }
  },
  getters:{
    //get
    getUser:state => {
      return state.user
    },
    getToken:state => {
      return state.token
    }
  },
  actions: {
  },
  modules: {
  }
})
